package com.flower.controller.buyer;

import com.flower.annotation.Log;
import com.flower.domain.dto.buyer.address.AddressDto;
import com.flower.domain.entity.Address;
import com.flower.service.AddressService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购买者端的收货地址管理
 */
@RestController
@RequestMapping("/buyer/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 获取收货地址列表
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getAddressList() {
        Long buyerId = UserContext.getUserId();
        List<Address> addresses = addressService.getAddressList(buyerId);
        return AjaxResult.success(addresses);
    }

    /**
     * 添加收货地址
     * @param addressDto
     * @return
     */
    @Log(name = "添加收货地址")
    @PostMapping("/add")
    public AjaxResult addAddress(@RequestBody AddressDto addressDto) {
        addressDto.setBuyerId(UserContext.getUserId().intValue());
        addressService.addAddress(addressDto);
        return AjaxResult.success();
    }

    /**
     * 更新收货地址
     * @param addressDto
     * @return
     */
    @Log(name = "更新收货地址")
    @PutMapping("/update")
    public AjaxResult updateAddress(@RequestBody AddressDto addressDto) {
        addressDto.setBuyerId(UserContext.getUserId().intValue());
        addressService.updateAddress(addressDto);
        return AjaxResult.success();
    }

    /**
     * 删除收货地址
     * @param addressId
     * @return
     */
    @Log(name = "删除收货地址")
    @DeleteMapping("/delete/{addressId}")
    public AjaxResult deleteAddress(@PathVariable("addressId") Integer addressId) {
        Long buyerId = UserContext.getUserId();
        addressService.deleteAddress(addressId, buyerId);
        return AjaxResult.success();
    }

    /**
     * 设置默认收货地址
     * @param addressId
     * @return
     */
    @PutMapping("/default/{addressId}")
    public AjaxResult setDefaultAddress(@PathVariable("addressId") Integer addressId) {
        Long buyerId = UserContext.getUserId();
        addressService.setDefaultAddress(addressId, buyerId);
        return AjaxResult.success();
    }
} 